package com.cloudweb.oa.controller.mobile;

import cn.js.fan.util.DateUtil;
import cn.js.fan.util.ParamUtil;
import com.cloudweb.oa.security.AuthUtil;
import com.cloudweb.oa.service.HomeService;
import com.redmoon.oa.person.PlanDb;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

@Api(tags = "日历（已弃用）")
@Slf4j
@Controller
public class CalendarController {

    @Autowired
    private HomeService homeService;

    @Autowired
    HttpServletRequest request;

    @Autowired
    AuthUtil authUtil;

    @RequestMapping(value = "/m/calendar/calendar")
    public String listPage(Model model) {
        model.addAttribute("nav", homeService.getNav());
        boolean isShared = ParamUtil.getBoolean(request, "isShared", false);
        String docTitle = "日程安排";
        if (isShared) {
            docTitle = "共享日程";
        }
        model.addAttribute("docTitle", docTitle);
        model.addAttribute("isShared", isShared);

        return "th/m/calendar/calendar";
    }

    @RequestMapping(value = "/m/calendar/addPage")
    public String addPage(Model model) {
        model.addAttribute("nav", homeService.getNav());
        com.redmoon.oa.pvg.Privilege oapvg = new com.redmoon.oa.pvg.Privilege();
        boolean canShare = oapvg.isUserPrivValid(request, "plan.share");
        model.addAttribute("canShare", canShare);
        boolean isUseSMS = com.redmoon.oa.sms.SMSFactory.isUseSMS();
        model.addAttribute("isUseSMS", isUseSMS);

        return "th/m/calendar/calendar_add";
    }

    @RequestMapping(value = "/m/calendar/showPage")
    public String showPage(Model model) {
        model.addAttribute("nav", homeService.getNav());

        int id = ParamUtil.getInt(request, "id", -1);
        PlanDb pd = new PlanDb();
        pd = pd.getPlanDb(id);
        String createDate = DateUtil.format(pd.getZdrq(), "yyyy-MM-dd");
        String bDate = DateUtil.format(pd.getMyDate(), "yyyy-MM-dd");
        String eDate = DateUtil.format(pd.getEndDate(), "yyyy-MM-dd");
        model.addAttribute("createDate", createDate);
        model.addAttribute("bDate", bDate);
        model.addAttribute("eDate", eDate);
        model.addAttribute("pd", pd);
        model.addAttribute("id", id);

        com.redmoon.oa.pvg.Privilege pvg = new com.redmoon.oa.pvg.Privilege();
        boolean canShare = pvg.isUserPrivValid(request, "plan.share");
        model.addAttribute("canShare", canShare);

        boolean canDo = true;
        if (!authUtil.getUserName().equals(pd.getUserName())) {
            if (!(pvg.canAdminUser(request, authUtil.getUserName()))) {
                canDo = false;
            }
        }
        model.addAttribute("canDo", canDo);

        return "th/m/calendar/calendar_show";
    }

    @RequestMapping(value = "/m/calendar/editPage")
    public String editPage(Model model) {
        model.addAttribute("nav", homeService.getNav());

        int id = ParamUtil.getInt(request, "id", -1);
        PlanDb pd = new PlanDb();
        pd = pd.getPlanDb(id);
        model.addAttribute("pd", pd);
        model.addAttribute("id", id);

        com.redmoon.oa.pvg.Privilege pvg = new com.redmoon.oa.pvg.Privilege();
        boolean canShare = pvg.isUserPrivValid(request, "plan.share");
        model.addAttribute("canShare", canShare);

        int before = DateUtil.datediffMinute(pd.getMyDate(), pd.getRemindDate());
        model.addAttribute("before", before);

        return "th/m/calendar/calendar_edit";
    }
}
